package cn.westv.scps.web.controller.ownerfeedback;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.ObjectUtils.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yunpian.sdk.constant.Config;

import cn.westv.scps.connTools.ProxyConnExec;
import cn.westv.scps.constant.ConfigCodeConstant;
import cn.westv.scps.domain.AreaSeverEntity;
import cn.westv.scps.domain.BikeOwnerEntity;
import cn.westv.scps.domain.OwnerFeedbackEntity;
import cn.westv.scps.service.bikeowner.IBikeOwnerService;
import cn.westv.scps.service.ownerfeedback.IOwnerFeedbackService;
import cn.westv.scps.utils.Page;
import cn.westv.scps.utils.StringUtils;
import cn.westv.scps.web.controller.base.AbstractController;
import cn.westv.scps.web.controller.ownerfeedback.vo.OwnerFeedbackVO;

/**
 * 用户反馈接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/sys/feedback")
public class OwnerFeedbackController extends AbstractController<OwnerFeedbackController>{
	
	@Autowired
	private IOwnerFeedbackService ownerFeedbackService;
	
	@Autowired
	private IBikeOwnerService bikeOwnerService;
	
	@RequestMapping("/index.cgi")
	public String toIndex_cgi() {
		return "forward:/WEB-INF/page/sys/feedback/feedbackIndex.jsp";
	}

	@RequestMapping("/edit.cgi")
	public String toEdit_cgi() {
		return "forward:/WEB-INF/page/sys/feedback/feedbackEdit.jsp";
	}
	
	@RequestMapping("/ownerdetail.cgi")
	public String toOwnerDetail_cgi(String id) {
		return "forward:/WEB-INF/page/sys/feedback/ownerDetail.jsp";
	}
	
	/**
	 * 分页查找
	 * @param ownerFeedbackVO
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/pagelist")
	public Map<String, Object> feedbackFindPage(OwnerFeedbackVO ownerFeedbackVO,Page<OwnerFeedbackEntity> page){
		try {
			String hql = "from OwnerFeedbackEntity where 1 = 1 ";
			List<Object> paramList = new ArrayList<Object>();
			OwnerFeedbackEntity ownerFeedbackEntity = ownerFeedbackVO.getOwnerFeedbackEntity();
			Date beginTime = ownerFeedbackVO.getBeginTime();
			Date endTime = ownerFeedbackVO.getEndTime();
			
			if (ownerFeedbackEntity != null) {
				String id = ownerFeedbackEntity.getId();
				if (StringUtils.isNotBlank(id)) {
					hql += " and" + " id = ?";
					paramList.add(id);
				}
				String content = ownerFeedbackEntity.getContent();
				if (StringUtils.isNotBlank(content)) {
					hql += " and" + " content like ?";
					paramList.add("%" + content + "%");
				}
				String feedbackAccount = ownerFeedbackEntity.getFeedbackAccount();
				if (StringUtils.isNotBlank(feedbackAccount)) {
					hql += " and" + " feedbackAccount like ?";
					paramList.add("%" + feedbackAccount + "%");
				}
				if (beginTime != null) {
					hql += " and" + " feedbackTime > ?";
					paramList.add(beginTime);
				}
				if (endTime != null ) {
					hql += " and" + " feedbackTime < ?";
					paramList.add(endTime);
				}
			}
			Page<OwnerFeedbackEntity> find = ownerFeedbackService.findPage(hql, page, OwnerFeedbackEntity.class, paramList.toArray());
			
			return ajaxReturnMap(OP_STATUS_SUCCESS, "search successful!", find);
			
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
	/**
	 * 保存
	 * @param ownerFeedbackVO
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> saveFeedback(OwnerFeedbackVO ownerFeedbackVO){
		try {
			String msg = "";//提示信息
			OwnerFeedbackEntity ownerFeedbackEntity = ownerFeedbackVO.getOwnerFeedbackEntity();
//			OwnerFeedbackEntity ownerFeedbackEntityDB = null;
			if(ownerFeedbackEntity != null){
				String id = ownerFeedbackEntity.getId();
				//id为空——添加
				if (StringUtils.isBlank(id)) {
//					ownerFeedbackEntityDB = ownerFeedbackEntity;
					Date date = new Date();
					ownerFeedbackEntity.setFeedbackTime(date);
					msg = ConfigCodeConstant.ADD_SUCCEED;//添加成功
					ownerFeedbackService.save(ownerFeedbackEntity);
				}else{
					//id不为空 —— 修改
//					ownerFeedbackEntityDB = ownerFeedbackService.get(OwnerFeedbackEntity.class, id);
//					ownerFeedbackEntityDB = ownerFeedbackEntity;
					//复制表单中的属性
//					String content = ownerFeedbackEntity.getContent();
//					ownerFeedbackEntityDB.setContent(content);
//					String feedbackAccount = ownerFeedbackEntity.getFeedbackAccount();
//					ownerFeedbackEntityDB.setFeedbackAccount(feedbackAccount);
					msg = ConfigCodeConstant.UPDATE_SUCCEED;//修改成功
					ownerFeedbackService.update(ownerFeedbackEntity);
				}
				return ajaxReturnMap(OP_STATUS_SUCCESS, msg, null);
			}else {
				return ajaxReturnMap(OP_STATUS_FAILD, "Save failed!", null);
			}
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/del")
	public Map<String, Object> delFeedback(String ids){
		try {
			String[] idArr = null;
			if (StringUtils.isNotBlank(ids)) {
				idArr = ids.split(","); //根据id批量删除，id之间使用","分隔
			}
			String msg = "";
			
			ownerFeedbackService.delete(OwnerFeedbackEntity.class, idArr);
			msg = ConfigCodeConstant.DEL_SUCCEED;
			return ajaxReturnMap(OP_STATUS_SUCCESS, msg, null);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
	}
	
	/**
	 * 根据id查找
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/detail")
	public Map<String, Object> detailFeedback(String id){
		if (StringUtils.isEmpty(id)) {
			return ajaxReturnMap(OP_STATUS_FAILD, ConfigCodeConstant.REQUIRED_PARAMETERS_IS_NULL + "（id为空）", null);
		}
		try {
			OwnerFeedbackEntity ownerFeedbackEntity = ownerFeedbackService.get(OwnerFeedbackEntity.class, id);
			return ajaxReturnMap(OP_STATUS_SUCCESS, "", ownerFeedbackEntity);
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
	}
	
	/**
	 * 获取查询列表（不分页）
	 * @param ownerFeedbackVO
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public List<OwnerFeedbackEntity> feedbackFind(OwnerFeedbackVO ownerFeedbackVO){
			String hql = "from OwnerFeedbackEntity where 1 = 1 ";
			List<Object> paramList = new ArrayList<Object>();
			OwnerFeedbackEntity ownerFeedbackEntity = ownerFeedbackVO.getOwnerFeedbackEntity();
			Date beginTime = ownerFeedbackVO.getBeginTime();
			Date endTime = ownerFeedbackVO.getEndTime();
			
			if (ownerFeedbackEntity != null) {
				String id = ownerFeedbackEntity.getId();
				if (StringUtils.isNotBlank(id)) {
					hql += " and" + " id = ?";
					paramList.add(id);
				}
				String content = ownerFeedbackEntity.getContent();
				if (StringUtils.isNotBlank(content)) {
					hql += " and" + " content like ?";
					paramList.add("%" + content + "%");
				}
				String feedbackAccount = ownerFeedbackEntity.getFeedbackAccount();
				if (StringUtils.isNotBlank(feedbackAccount)) {
					hql += " and" + " feedbackAccount like ?";
					paramList.add("%" + feedbackAccount + "%");
				}
				if (beginTime != null) {
					hql += " and" + " feedbackTime > ?";
					paramList.add(beginTime);
				}
				if (endTime != null ) {
					hql += " and" + " feedbackTime < ?";
					paramList.add(endTime);
				}
			}
			List<OwnerFeedbackEntity> find = ownerFeedbackService.find(hql, OwnerFeedbackEntity.class, paramList.toArray());
			
			return find;
	}
	
	/**
	 * 根据电话号码查询用户
	 * @param phone
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/searchowner")
	public Map<String, Object> searchOwnerByPhone(String id){
		try {
			OwnerFeedbackEntity ownerFeedbackEntity = ownerFeedbackService.get(OwnerFeedbackEntity.class, id);
			if (ownerFeedbackEntity != null ) {
				String mobile = ownerFeedbackEntity.getFeedbackAccount();
				BikeOwnerEntity bikeOwner = bikeOwnerService.findByMobile(mobile);
				if (bikeOwner != null) {
					AreaSeverEntity server = bikeOwner.getAreaSever();
					if (server != null) {
						JSONObject owner = ProxyConnExec.getOwnerToCloudbase(mobile, server);
						owner.put("feedbackAccount", ownerFeedbackEntity.getFeedbackAccount());
						owner.put("content", ownerFeedbackEntity.getContent());
						return ajaxReturnMap(OP_STATUS_SUCCESS, "", owner);
					}else {
						return ajaxReturnMap(OP_STATUS_FAILD,"服务器不存在！",null);
					}
				}else{
					return ajaxReturnMap(OP_STATUS_FAILD,"账号不存在！",null);
				}
			}else {
				return ajaxReturnMap(OP_STATUS_FAILD,"id不存在！",null);
			}
		} catch (Exception e) {
			logErr(e);
			return ajaxReturnMap(OP_STATUS_FAILD, returnMsg(ConfigCodeConstant.UNKNOWN_ERR), null);
		}
		
	}
	
}
